<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * 站点管理模型类
 * @author 66diqiu-li
 *
 */
class Admin_Site_model extends CI_Model {

	public function __construct(){
		parent::__construct();
		$this->load->driver('cache');
	}

	/**
	 * 
	 * 更新数据
	 * @param array $data 数据数组
	 * @param array $where 更新条件 
	 */
	function edit_row($data,$where){
		$table = $this->get_table_name();
	    if(!empty($where))$this->db->where($where, NULL, FALSE);
	    return $this->db->update($table,$data);
	}
	
	/**
	 * 
	 * 根据域名获取站点信息
	 * @param $host
	 */
	function get_site_info($host){
		$host = rtrim($host,'/');
		static $get_site_info = array();
		if(isset($get_site_info[$host])){
			return $get_site_info[$host];
		}

		$cache_key = 'h:'.md5($host);
		if(!$res = $this->cache->file->get($cache_key)) {
			$res = $this->get_row(array('url' => $host));
			if($res){
				$this->cache->file->save($cache_key, $res, 7200);
			}
		}
		$get_site_info[$host] = $res;
		return $get_site_info[$host];
	}

    function get_site_info_by_adminname($name, $sid){
        return $this->get_row(array('founder'=>$name, 'sid'=> $sid));
    }

	/**
	 * 
	 * 添加数据
	 * @param $data
	 */
	function add_row($data){
        if(empty($data['url'])){
            return false;
        }
        $data['dateline'] = date('Y-m-d H:i:s');
	    $table = $this->get_table_name();
	    if(is_array($data)){
	        if($this->db->insert($table,$data)){
                $sid = $this->db->insert_id();
	            return $sid ? $sid : $data['sid'];
	        }else{
	            return false;
	        }
	    }
	}
	
	/**
	 * 
	 * 获取单条记录
	 * @param array $where 查询条件
	 */
	function get_row($where=array(),$field='*'){
	    $table = $this->get_table_name();
	    if(!empty($field))$this->db->select($field);
	    $query = $this->db->get_where($table,$where,1);
	    return $query->row_array();
	}
	
	/**
	 * 
	 * 删除数据
	 * @param array $where 删除条件
	 */
	function remove_row($where=array()){
		$table = $this->get_table_name();
		if(!empty($where))$this->db->where($where, NULL, FALSE);		
		return $this->db->delete($table);
	}
		
	/**
	 * 
	 * 获取总数量
	 * @param array $where 查询条件
	 */
	function get_count($where=array()){
        $table = $this->get_table_name();
        if(!empty($where))$this->db->where($where, NULL, FALSE);
        $this->db->select('id');
        $this->db->from($table);
        return $this->db->count_all_results();        
	}
	
	/**
	 * 
	 * 获取列表
	 */
	function get_site_list($status=null,$ids=null, $linshi = null, $page_size = 20, $now_page = 1){
	    $table = $this->get_table_name();
        if($status !== null)
        {
            $this->db->where('status',$status);
        }

		if($ids !==null){
			if($ids==''){
				$ids='0';
			}
			$this->db->where('sid in ('.trim($ids,",").')');
		}

		$linurl = addslashes('http://www.66diqiu.com%');
		if($linshi !== null){
			if($linshi){
				$this->db->where("url LIKE '$linurl'");
			}else{
				$this->db->where("url NOT LIKE '$linurl'");
			}
		}
	    $this->db->select('*');
	    $this->db->order_by('sid DESC');
//		$start = $page_size*($now_page-1);
//		$this->db->limit($page_size, $start);
	    $query = $this->db->get($table);
	    $data = array();
	    foreach($query->result_array() as $row){
	    	$data[] = $row;
	    }
	    return $data;
	}

	function get_list($page_size = 20, $now_page = 1, $where){
		$table = $this->get_table_name();
		$table_config = $this->db->dbprefix("shop_config_site");
		$sql = "select a.name,a.url,a.sid from $table as a left join $table_config as b on b.sid=a.sid and b.key_code='siteprovince' left join $table_config as c on c.sid=a.sid and c.key_code='sitecity' where a.status=1 $where ";
		$sql_count = "select count(a.sid) as num from $table as a left join $table_config as b on b.sid=a.sid and b.key_code='siteprovince' left join $table_config as c on c.sid=a.sid and c.key_code='sitecity' where a.status=1 $where ";
		$query_all = $this->db->query($sql_count);
		$total = $query_all->row_array();
		$start = $page_size*($now_page-1);
		$sql.= " limit $start,$page_size ";
		$query = $this->db->query($sql);
		$list = $query->result_array();
		return array('list'=>$list,'count'=>$total['num']);
	}
	
    /**
     * 
     * 获取数据表名称
     */
    function get_table_name(){
        $table = $this->db->dbprefix("siteinfo");
        return $table;
    }

	public function get_site_name_by_siteid($sid)
	{
		static $siteids = array();
		if($siteids[$sid]){
			return $siteids[$sid];
		}
		$ret = $this->get_row(array('sid'=> $sid));
		$siteids[$sid] = $ret['name'];
		return $siteids[$sid];
	}

	public function get_max_port()
	{
		$table = $this->get_table_name();
		$this->db->select('url');
		$this->db->order_by('sid DESC');
		$query = $this->db->get($table);
		$data = array();
		foreach($query->result_array() as $row){
			$row['url'] = str_replace('http://', '', $row['url']);
			if(strpos($row['url'], ':') === false){
				continue;
			}
			list($url, $port ) = explode(':', $row['url']);
			if($port != 32753){
				$data[] = $port;
			}
		}
		return max($data);
	}
}